<template>
  <el-card class="mb-2.5" v-show="SearchShow">
    <!-- 搜索表单 -->
    <el-form ref="searchFormRef" :model="searchForm" label-position="left">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="用户名">
            <el-input v-model="searchForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" :icon="Search" plain @click="handleSearch">搜索</el-button>
          <el-button :icon="RefreshRight" @click="handleSearchFormReset">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <el-card>
    <!-- 数据表格 -->
    <div class="table-container">
      <div class="mb-2.5">
        <el-row :gutter="20">
          <el-col :span="12">
          </el-col>
          <el-col :span="12">
            <div class="flex justify-end">
              <el-button :icon="Search" circle @click="SearchShow = !SearchShow"/>
              <el-button :icon="RefreshRight" circle @click="handleSearchFormReset"></el-button>
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" style="width: 100%;">
          <el-table-column align="center" prop="id" label="ID"></el-table-column>
          <el-table-column align="center" prop="admin.username" label="用户名"></el-table-column>
          <el-table-column align="center" prop="method" label="请求方式"></el-table-column>
          <el-table-column align="center" prop="route" label="请求路由"></el-table-column>
          <el-table-column align="center" prop="real_name" label="业务名称"></el-table-column>
          <el-table-column align="center" prop="ip" label="请求IP"></el-table-column>
          <el-table-column align="center" prop="create_time" label="创建时间"></el-table-column>
          <el-table-column align="center" label="操作">
            <template #default="scope">
              <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页组件 -->
      <div class="py-5 px-0 flex justify-end">
        <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[15, 30, 50, 100]"
                       :disabled="false" background layout="total, sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue';
import {RefreshRight, Search, CirclePlus} from '@element-plus/icons-vue'
import {getOperateLogListApi, deleteOperateLogInfoApi} from '@/api/operateLog'
import { ElMessageBox, ElMessage } from 'element-plus';

// 表格数据
const tableData = ref([]);
// 表格数据总数量
const total = ref(0);
// 当前分页
const currentPage = ref(1);
// 项目条数
const pageSize = ref(15);
// 是否显示搜索表单视图
const SearchShow = ref(true);

// 定义搜索表单的类型
interface SearchForm {
  username?: string; // 可选属性，表示用户名字段
}

// 初始化 searchForm 时明确类型并提供默认值
const searchForm = ref<SearchForm>({});

// 加载表格数据
const loadTableData = async () => {
  let params = {
    page: currentPage.value,
    page_size: pageSize.value,
  }
  const response = await getOperateLogListApi({
    ...params, // 动态传递分页参数或搜索条件
    ...searchForm.value, // 拼接搜索条件
  });
  tableData.value = response.data.list;
  total.value = response.data.total;
};

// 搜索逻辑
const handleSearch = () => {
  currentPage.value = 1; // 搜索后重置到第一页
  loadTableData(); // 调用加载方法并传递分页参数
};

// 重置搜索表单
const handleSearchFormReset = () => {
  searchForm.value = {};
  currentPage.value = 1; // 重置后也回到第一页
  loadTableData(); // 重新加载初始数据
};

// 删除行数据
const handleDelete = (row) => {
  ElMessageBox.confirm(
      '确定删除此数据吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(async () => {
    // 执行删除操作
    await deleteOperateLogInfoApi(row.id);
    // 刷新表格数据
    await loadTableData();
    // 提示删除成功
    ElMessage({
      type: 'success',
      message: '删除成功',
    });
  })
};

// 表格行数变化处理
const handleSizeChange = async (page: number) => {
  pageSize.value = page;
  await loadTableData()
}

// 分页变化处理
const handleCurrentChange = async (newPage) => {
  currentPage.value = newPage;
  await loadTableData()
};

onMounted(() => {
  loadTableData(); // 在组件挂载后加载数据
});
</script>

<style scoped>
.table-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.table-wrapper {
  flex-grow: 1;
  overflow-y: auto;
  margin-bottom: 10px;
}
</style>